//
//  CancelOrderViewController.m
//  DaJiang
//
//  Created by sally on 16/10/12.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CancelOrderViewController.h"
#import "CellManager.h"
#import "OrderModel.h"
#import "OrderSingleSelectCell.h"
#import "UITextView+Placeholder.h"

@interface CancelOrderViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) NSIndexPath *selectIndexPath; //选中的原因
@property (strong, nonatomic) UITextView *textView;         //填写的原因

@end

@implementation CancelOrderViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"请选择取消原因";
  self.footerView.height = 74;
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
  [self.tableData addObject:@[ @"时间安排有变", @"拍错了话题", @"我的问题已解决，谢谢" ]];
  [self.tableData addObject:@[ @"其它原因", @"" ]];
}

#pragma mark - action
#pragma mark 点击提交
- (IBAction)actionOnSend:(id)sender {
  NSString *reason = @"";
  if (self.selectIndexPath) {
    reason = [[self.tableData objectAtIndex:self.selectIndexPath.section]objectAtIndex:self.selectIndexPath.row];
  }else if (self.textView.text.length > 0){
    reason = self.textView.text;
  }
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];

  [OrderModel userCancelOrderWithReason:reason consultId:self.consultId completion:^(BOOL isSucceed) {
    if (isSucceed) {
      UIWindow *window = [UIApplication sharedApplication].keyWindow;
      [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已取消订单"];
      [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
      NSError *error = [OrderModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;

  }];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[self.tableData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.section == 1 && indexPath.row == 1) { //最后一项
    return GTextViewHeight - NavHeight;
  }
  return NavHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.section == 1 && indexPath.row == 1) { //最后一项
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //编辑框
    self.textView = [CellManager configCellTextView:cell];
    self.textView.placeholder = @"请填写取消原因";
    [cell.contentView addSubview:self.textView];
    return cell;

  } else {
    OrderSingleSelectCell *cell = (OrderSingleSelectCell *) [tableView dequeueReusableCellWithIdentifier:@"OrderSingleSelectCell"];
    if (!cell) {
      cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderSingleSelectCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;

    cell.titleLabel.text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 1) {
      cell.titleLabel.textColor = GGrayWordColor1;
    }

    if (indexPath == self.selectIndexPath) {
      cell.selectBtn.selected = YES;
    } else {
      cell.selectBtn.selected = NO;
    }

    return cell;
  }
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  //选中原因变色
  self.selectIndexPath = indexPath;
  [self.tableView reloadData];
}
@end
